home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libobj / box.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.3 KB  |  163 lines

  1. /*
  2.  * box.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * box.c,v 4.1 1994/08/09 07:58:10 explorer Exp
  17.  *
  18.  * box.c,v
  19.  * Revision 4.1  1994/08/09  07:58:10  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:08  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:36:32  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "geom.h"
  30. #include "box.h"
  31.  
  32. static Methods *iBoxMethods = NULL;
  33. static char boxName[] = "box";
  34.  
  35. unsigned long BoxTests, BoxHits;
  36.  
  37. Box *
  38. BoxCreate(v1, v2)
  39. Vector *v1, *v2;
  40. {
  41.     Box *box;
  42.     Vector size;
  43.  
  44.     VecSub(*v1, *v2, &size);
  45.  
  46.     if (equal(size.x, 0.) || equal(size.y, 0.) || equal(size.z, 0.)) {
  47.         RLerror(RL_WARN, "Degenerate box.\n");
  48.         return (Box *)NULL;
  49.     }
  50.  
  51.     box = (Box *)share_malloc(sizeof(Box));
  52.     box->bounds[LOW][X] = min(v1->x, v2->x);
  53.     box->bounds[HIGH][X] = max(v1->x, v2->x);
  54.     box->bounds[LOW][Y] = min(v1->y, v2->y);
  55.     box->bounds[HIGH][Y] = max(v1->y, v2->y);
  56.     box->bounds[LOW][Z] = min(v1->z, v2->z);
  57.     box->bounds[HIGH][Z] = max(v1->z, v2->z);
  58.     return box;
  59. }
  60.  
  61. Methods *
  62. BoxMethods()
  63. {
  64.     if (iBoxMethods == (Methods *)NULL) {
  65.         iBoxMethods = MethodsCreate();
  66.         iBoxMethods->create = (GeomCreateFunc *)BoxCreate;
  67.         iBoxMethods->methods = BoxMethods;
  68.         iBoxMethods->name = BoxName;
  69.         iBoxMethods->intersect = BoxIntersect;
  70.         iBoxMethods->normal = BoxNormal;
  71.         iBoxMethods->enter = BoxEnter;
  72.         iBoxMethods->bounds = BoxBounds;
  73.         iBoxMethods->stats = BoxStats;
  74.         iBoxMethods->checkbounds = FALSE;
  75.         iBoxMethods->closed = TRUE;
  76.     }
  77.     return iBoxMethods;
  78. }
  79.  
  80. int
  81. BoxIntersect(box, ray, mindist, maxdist)
  82. Box *box;
  83. Ray *ray;
  84. Float mindist, *maxdist;
  85. {
  86.     BoxTests++;
  87.     if (BoundsIntersect(ray, box->bounds, mindist, maxdist)) {
  88.         BoxHits++;
  89.         return TRUE;
  90.     }
  91.     return FALSE;
  92. }
  93.  
  94. int
  95. BoxNormal(box, pos, nrm, gnrm)
  96. Vector *pos, *nrm, *gnrm;    /* point of intersection */
  97. Box *box;
  98. {
  99.     nrm->x = nrm->y = nrm->z = 0.;
  100.  
  101.     if (equal(pos->x, box->bounds[HIGH][X]))
  102.         nrm->x = 1.;
  103.     else if (equal(pos->x, box->bounds[LOW][X]))
  104.         nrm->x = -1.;
  105.     else if (equal(pos->y, box->bounds[HIGH][Y]))
  106.         nrm->y = 1.;
  107.     else if (equal(pos->y, box->bounds[LOW][Y]))
  108.         nrm->y = -1.;
  109.     else if (equal(pos->z, box->bounds[HIGH][Z]))
  110.         nrm->z = 1.;
  111.     else if (equal(pos->z, box->bounds[LOW][Z]))
  112.         nrm->z = -1.;
  113.     else
  114.         RLerror(RL_WARN, "Confusion in nrmbox!\n");
  115.     *gnrm = *nrm;
  116.     return FALSE;
  117. }
  118.  
  119. /*
  120.  * Determine if ray enters (TRUE) or leaves (FALSE) box at pos
  121.  */
  122. int
  123. BoxEnter(box, ray, mind, hitd)
  124. Box *box;
  125. Ray *ray;
  126. Float mind, hitd;
  127. {
  128.     Vector pos;
  129.  
  130.     VecAddScaled(ray->pos, mind, ray->dir, &pos);
  131.     return OutOfBounds(&pos, box->bounds);
  132. }
  133.  
  134. void
  135. BoxBounds(box, bounds)
  136. Box *box;
  137. Float bounds[2][3];
  138. {
  139.     BoundsCopy(box->bounds, bounds);
  140. }
  141.  
  142. char *
  143. BoxName()
  144. {
  145.     return boxName;
  146. }
  147.  
  148. void
  149. BoxStats(tests, hits)
  150. unsigned long *tests, *hits;
  151. {
  152.     *tests = BoxTests;
  153.     *hits = BoxHits;
  154. }
  155.  
  156. void
  157. BoxMethodRegister(meth)
  158. UserMethodType meth;
  159. {
  160.     if (iBoxMethods)
  161.         iBoxMethods->user = meth;
  162. }
  163.